using Admin2025.Domain.Repositories;
using Admin2025.infrastructure.Repositories;
using Admin2025.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Admin2025.Infrastructure;

/// <summary>
/// 提供基础设施层相关服务的扩展方法。
/// </summary>
public static class ServiceCollectionExtensions
{
    /// <summary>
    /// 向依赖注入容器中注册基础设施相关服务（如DbContext和仓储）。
    /// </summary>
    /// <param name="services">服务集合。</param>
    /// <param name="configuration">应用配置。</param>
    /// <returns>服务集合本身，支持链式调用。</returns>
    public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
    {
        // 获取MySQL数据库连接字符串
        var DbContextConfig = configuration.GetConnectionString("MySql");
        
        // 注册AdminDbContext，并配置为使用MySQL数据库
        services.AddDbContext<AdminDbContext>(options => options.UseMySql(DbContextConfig, ServerVersion.AutoDetect(DbContextConfig)));
        
        // 注册泛型仓储接口及其实现
        services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
        
        return services;
    }
}